# 容器类型排序
import datetime
import json
import logging
import os
import shutil
import time
import os.path as op


class User:

    def __init__(self, user_id, name, age, addr):
        self.userId = user_id
        self.name = name
        self.age = age
        self.addr = addr

    def __str__(self):
        user_str = "{'userId':{0},'name':{1},'age':{2},'addr':{3}}"
        return user_str.format(self.userId, self.name, self.age, self.addr)


user_list = [User(1, "xueXue", 15, "湖北省黄石市阳新县龙港镇白领村"),
             User(2, "yapYao", 18, "湖北省咸宁市崇阳县"),
             User(4, "lily", 23, "湖北省仙桃市"),
             User(3, "lily", 23, "上海市徐汇区")]


# 复杂类型排序 先按照age 倒序排列 在按照userId正序排序
# print(json.dumps(sorted(user_list, key=lambda x: (-x.age, x.userId)), default=lambda obj: obj.__dict__,
#                  ensure_ascii=False, indent=2))

# 文件操作

# 打开文件
# with open("User.java", mode='r') as f:
#     # 按照行读取全部并且转入集合
#     # print(f.readlines())
#     while True:
#         # 读取指定的字节数
#         # ret = f.read(1024)
#         # 读取指定全部文件
#         # ret = f.read()
#         # 按行读取
#         ret = f.readline()
#         print(ret, end="")
#         if not ret:
#             break
#     f.close()
#
# with open("User.java", mode='a') as f:
#     # 按照写入
#     f.writelines('   private int age;')
#     f.close()
# 搜集异常信息并且写入到文件
def lear_01(x, y):
    try:
        return x // y
    except Exception as e:
        with open("record_error.txt", "a") as f:
            f.writelines(time.strftime('%Y-%m-%d %H:%M:%d', time.localtime(time.time())) + ":----->" + repr(e) + "\n")
            f.close()


# lear_01(10, 0)

# 常用操作文件方法
# 修改文件名称 当文件已存在时，无法创建该文件。: 'record_error.txt' -> 'exception.txt'
# os.rename("record_error.txt", "exception1.txt")
# 创建文件
# os.mkdir("test")
# open("test/1.txt","w")
# 删除文件
# os.remove("exception1.txt")
# 删除文件夹
# os.rmdir("test")
# 递归删除文件
# shutil.rmtree("test")
# 获取当前工作工具目录
print(os.getcwd())
# 修改当前工作目录到指定目录上去
os.chdir("C:\\Users\\105286\\Desktop")
print(os.getcwd())
# 获取当前目录下的文件
print(os.listdir())

# shutil.move("store-billing.bak", "store-billing")

# shutil.copytree("store-billing", "store-billing.bak")

# 自己实现cp命令

# 批量赋值文件
# def batch_cp(s,):
#     op.isdir()
